<style scoped>
.img-file {
  height: 200px;
}
</style>
<template>
  <div>

    <vxe-toolbar ref="toolbarRef" custom>

      <template #buttons>
        <Button type="primary" @click="AddData" icon="md-add">新增</Button>

      </template>
    </vxe-toolbar>
    <vxe-table border ref="xTable" id="system_appmanage" :height="tableHeight" :loading="loadingTable"  
      size="small" align="center" :row-config="{ isHover: true }" :data="tableData" show-header-overflow
      show-overflow="tooltip">
      <vxe-column type="seq" title="序号" width="60">
      </vxe-column>
      <vxe-column title="操作" fixed="right" width="100">
        <template #default="{ row }">
          <Button icon="ios-create" style="margin-right:5px" @click="tempData = row; modalData = true"></Button>
          <Button icon="ios-trash" @click="DeleteAppInfo(row.Id)"></Button>
        </template>
      </vxe-column>



      <vxe-column field="AppName" title="app名称" width="200"></vxe-column>
      <vxe-column field="PackageName" title="包名" width="200"></vxe-column>
      <vxe-column field="AndroidVersion" title="andorid版本" width="200"> </vxe-column>
      <vxe-column field="IosVersion" title="ios版本" width="200"> </vxe-column>
      <vxe-column field="VersonName" title="版本信息" width="200"> </vxe-column>
      <vxe-column field="UpdateType" title="强制更新" width="200"> </vxe-column>
      <vxe-column field="UpdateContent" title="更新内容" min-width="200"> </vxe-column>

    </vxe-table>

    <Modal :width="600" v-model="modalData" sticky draggable :title="tempData.Id > 0 ? '修改' : '新增'"
      :mask-closable="false">
      <Form ref="formValidate" :model="tempData" :label-width="120" :rules="ruleValidate">
        <FormItem label="app名称" prop="AppName">
          <Input v-model="tempData.AppName" />
        </FormItem>
        <FormItem label="包名" prop="PackageName">
          <Input v-model="tempData.PackageName" />
        </FormItem>
        <FormItem label="android版本号" prop="AndroidVersion">
          <Input v-model="tempData.AndroidVersion" />
        </FormItem>

        <FormItem label="apk上传" prop="AndroidVersion">
          <Upload ref="upload" :show-upload-list="false" :on-success="handleSuccess" :format="['apk', 'ico']"
            :on-format-error="handleFormatError" :max-size="10240000" :on-exceeded-size="handleMaxSize"
            :action="uploadurl" :headers="uploadheader">
            <Button icon="ios-cloud-upload-outline">上传</Button>
          </Upload>
          <div>{{ tempData.DownLoadUrl }}</div>


        </FormItem>
        <FormItem label="IOS版本号" prop="IosVersion">
          <Input v-model="tempData.IosVersion" />
        </FormItem>

        <FormItem label="版本名称" prop="VersonName">
          <Input v-model="tempData.VersonName" />
        </FormItem>

        <FormItem label="IOS商店地址" prop="AppStoreUrl">
          <Input v-model="tempData.AppStoreUrl" />
        </FormItem>
        <FormItem label="更新类型">
          <RadioGroup v-model="tempData.UpdateType">
            <Radio :label="0">正常更新</Radio>
            <Radio :label="1">强制更新</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="更新内容" prop="VersonName">
          <Input type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" v-model="tempData.UpdateContent" />
        </FormItem>


      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SaveAppInfo">确定</Button>
      </div>
    </Modal>

  </div>
</template>

<script>
import { getToken, baseUrl, fileUrl } from '@/libs/util'
import {
  GetAppInfoList,
  SaveAppInfo,
  DeleteAppInfo
} from '@/api/system'

export default {
  name: 'appmanage',
  data() {
    return {
      baseUrl,
      fileUrl,
      uploadurl: baseUrl + 'api/File/UploadFile',
      uploadheader: {
        Authorization: 'Bearer ' + getToken()
      },
      total: 0,
      page: 1,
      size: 50,
      tableHeight: 450,
      keyword: '',
      ruleValidate: {
        PackageName: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      loadingTable: false,
      tempData: {
        PackageName: '',
        DownLoadUrl: ''
      },
      modalData: false,
      modalMenu: false,

      tableData: []

    }
  },
  computed: {},
  methods: {
    handleSuccess(res, file) {
      let vm = this
      if (res.StatusCode == 200) {
        vm.tempData.DownLoadUrl = res.Data.Url
      } else {
        this.$Notice.error({
          title: '上传失败',
          desc: res.Message
        })
      }
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: '格式不正确'
      })
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: '文件大小不能超过100M'
      })
    },
    SaveAppInfo() {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true

          SaveAppInfo(vm.tempData).then((res) => {
            vm.loading = false

            if (res.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.GetAppInfoList()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },
    GetAppInfoList() {
      let vm = this
      vm.loadingTable = true
      GetAppInfoList().then((res) => {
        vm.loadingTable = false

        if (res.StatusCode == 200) {
          vm.tableData = res.Data
        }
      })
    },
    DeleteAppInfo(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeleteAppInfo(id).then((res) => {
            if (res.StatusCode == 200) {
              this.GetAppInfoList()
              this.$Message.success({ title: '提示', content: '删除成功' })
            }
          })
        },
        onCancel: () => { }
      })
    },
    AddData() {
      var vm = this
      vm.modalData = true
      vm.tempData = {
        PackageName: '',
        DownLoadUrl: ''
      }
    }

  },
  mounted() {
    this.tableHeight = window.innerHeight - 240
    const $table = this.$refs.xTable
    const $toolbar = this.$refs.toolbarRef
    if ($table && $toolbar) {
      $table.connect($toolbar)
    }
    this.GetAppInfoList()
  }
}
</script>

<style></style>
